﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
using SpectrumAnalyser.Common.Hosting;
using SpectrumAnalyser.Data.DataBase;
using SpectrumAnalyser.Hosts.Common;
using SpectrumAnalyser.ModelObjects.Enum;
using SpectrumAnalyser.Services.ExpertiseResultStorageService;
using SpectrumAnalyser.Services.ExpertiseResultStorageService.Services;
using SpectrumAnalyser.Services.ExpertiseResultStorageServiceContracts;

namespace SpectrumAnalyser.Hosts.ExpertiseResultStorageServiceHost
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("Press any key to stop");
            var unityContainer = new UnityContainer();

            unityContainer.AddNewExtension<Interception>();
#if DEBUG

            var hostUri = WcfHelper.GetEndpointUri(UriScheme.Http, "IConfigService");
            args = new[] { hostUri.ToString() };
#else
    //TODO: hostUri from args[].
#endif

            unityContainer.RegisterType<ServiceHostBoostrapperBase, ExpertiseResultStorageBootstrapper>();

            unityContainer.Resolve<ExpertiseResultStorageServiceHost>().Run(args);

            Console.ReadLine();
        }
    }

    public class ExpertiseResultStorageServiceHost : ServiceHostBase
    {
        public ExpertiseResultStorageServiceHost(IUnityContainer unityContainer)
            : base(unityContainer)
        {
        }

        protected override IDictionary<Type, Type> ServiceTypes()
        {
            return new Dictionary<Type, Type>
                {
                    {typeof (IExpertiseStorageServiceContract), typeof (ExpertiseStorageService)}
                };
        }
    }

    public class ExpertiseResultStorageBootstrapper : ServiceHostBoostrapperBase
    {
        private readonly IUnityContainer _unityContainer;

        public ExpertiseResultStorageBootstrapper(IUnityContainer unityContainer)
            : base(unityContainer)
        {
            this._unityContainer = unityContainer;
        }

        public override void Initialize()
        {
            base.Initialize();

            this._unityContainer.Resolve<DataModule>().Initialize();
            this._unityContainer.Resolve<ExpertiseResultStorageServiceModule>().RegisterServices();
        }
    }
}
